package info.vokabel;

import java.io.File;
import javax.swing.JOptionPane;

/**
 * @author msonderm
 */
public class MainVokabel extends javax.swing.JFrame {
EListe liste;
boolean testdir_d_e=true;
int test_position=0;
boolean adding=false;
VokabelElement element;
   
    public MainVokabel() {
        initComponents();
        liste=new EListe("vok.txt");//new File("salat.txt").getAbsolutePath());
        liste.loadSelf();
        updateFields(liste.aktuellesElement());
    }

   
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        bnClear = new javax.swing.JButton();
        tbDeutsch = new javax.swing.JTextField();
        bnDel = new javax.swing.JButton();
        bnNew = new javax.swing.JButton();
        lbStatus = new javax.swing.JLabel();
        cbShowVok = new javax.swing.JCheckBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbVokabeln = new javax.swing.JTextPane();
        bnBeginn = new javax.swing.JButton();
        bnBack = new javax.swing.JButton();
        bnFor = new javax.swing.JButton();
        bnEnd = new javax.swing.JButton();
        bnDir = new javax.swing.JButton();
        tbEnglisch = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        bnTest = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        bnClear.setText("Liste Leeren");
        bnClear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bnClearActionPerformed(evt);
            }
        });

        bnDel.setText("Aktuelle Vokabel Löschen");
        bnDel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bnDelActionPerformed(evt);
            }
        });

        bnNew.setText("Vokabel hinzufügen");
        bnNew.setToolTipText("");
        bnNew.setActionCommand("");
        bnNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bnNewActionPerformed(evt);
            }
        });

        lbStatus.setText(" ");

        cbShowVok.setText("Vokabeln anzeigen");
        cbShowVok.setFocusable(false);
        cbShowVok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbShowVokActionPerformed(evt);
            }
        });

        jScrollPane1.setViewportView(tbVokabeln);

        bnBeginn.setText("<|--");
        bnBeginn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bnBeginnActionPerformed(evt);
            }
        });

        bnBack.setText("<-");
        bnBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bnBackActionPerformed(evt);
            }
        });

        bnFor.setText("->");
        bnFor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bnForActionPerformed(evt);
            }
        });

        bnEnd.setText("--|>");
        bnEnd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bnEndActionPerformed(evt);
            }
        });

        bnDir.setText("->");
        bnDir.setFocusable(false);
        bnDir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bnDirActionPerformed(evt);
            }
        });

        jLabel3.setLabelFor(tbEnglisch);
        jLabel3.setText("Englisch");

        jLabel4.setText("Deutsch");

        bnTest.setText("Überprüfen");
        bnTest.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bnTestActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Phase 7");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(bnClear)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(bnDel)
                        .addGap(65, 65, 65)
                        .addComponent(bnNew))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(bnBeginn)
                        .addGap(18, 18, 18)
                        .addComponent(bnBack)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(bnFor)
                        .addGap(18, 18, 18)
                        .addComponent(bnEnd))
                    .addComponent(cbShowVok)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(4, 4, 4)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(tbDeutsch, javax.swing.GroupLayout.PREFERRED_SIZE, 217, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(lbStatus)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(bnTest))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(bnDir, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel3)
                                    .addComponent(tbEnglisch, javax.swing.GroupLayout.PREFERRED_SIZE, 217, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(0, 0, Short.MAX_VALUE)))))
                .addContainerGap())
            .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(4, 4, 4)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bnClear)
                    .addComponent(bnDel)
                    .addComponent(bnNew))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(17, 17, 17)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(bnBeginn)
                            .addComponent(bnBack)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(bnEnd)
                            .addComponent(bnFor))))
                .addGap(22, 22, 22)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tbDeutsch, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bnDir)
                    .addComponent(tbEnglisch, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbStatus)
                    .addComponent(bnTest))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(cbShowVok)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 170, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cbShowVokActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbShowVokActionPerformed
        if(cbShowVok.isSelected()){
            updateList();
        } else {
            tbVokabeln.setText("Vokabelliste Ausgeblendet");
        }
    }//GEN-LAST:event_cbShowVokActionPerformed

    private void bnNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bnNewActionPerformed
        if(!adding){
           adding=true;
           bnNew.setText("Bearbeitung abschließen");
           tbEnglisch.setEnabled(true);
           tbDeutsch.setEnabled(true);
           tbDeutsch.setText("");
           tbEnglisch.setText("");
        } else {
           adding=false;
           bnNew.setText("Vokabel hinzufügen");
           add();
           updateFields(liste.aktuellesElement());
        }
        updateList();
    }//GEN-LAST:event_bnNewActionPerformed

    public void add(){
       String d=tbDeutsch.getText();
       String e=tbEnglisch.getText();
       if("".equals(d)){
          showError("Keine Deutsche Übersetzung angegeben!");
          return;
       } 
       if("".equals(e)){
          showError("Keine Englische Übersetzung angegeben!");
          return;
       }
       liste.einfuegen(new VokabelElement(d,e));
       liste.addNewT(element);
       updateList();
    }
    
    private void bnDirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bnDirActionPerformed
        if(bnDir.getText().equals("->")){
            testdir_d_e=false;
            bnDir.setText("<-");
        } else {
            testdir_d_e=true;
            bnDir.setText("->");
        }
        updateFields(liste.aktuellesElement());
    }//GEN-LAST:event_bnDirActionPerformed

    private void bnTestActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bnTestActionPerformed
       String d=tbDeutsch.getText();
       String e=tbEnglisch.getText();
       if(!d.equals(element.getDeutsch())){
           lbStatus.setText("Deutsch Falsch!");
       } else if (!e.equals(element.getEnglisch())){
           lbStatus.setText("Englisch Falsch!");
       } else {
           lbStatus.setText("Korrekt!");
       }
    }//GEN-LAST:event_bnTestActionPerformed

    private void bnForActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bnForActionPerformed
        if(test_position<liste.gibLänge()-1){
            test_position++;
            liste.naechstesElement();
            updateFields(liste.aktuellesElement());
        }
    }//GEN-LAST:event_bnForActionPerformed

    private void bnBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bnBackActionPerformed
        if(test_position>0){
            test_position--;
            liste.goTo(test_position);
            updateFields(liste.aktuellesElement());
        }
        //updateFields(liste.aktuellesElement());
    }//GEN-LAST:event_bnBackActionPerformed

    private void bnBeginnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bnBeginnActionPerformed
        test_position=0;
        liste.zumAnfang();
        updateFields(liste.aktuellesElement());
    }//GEN-LAST:event_bnBeginnActionPerformed

    private void bnEndActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bnEndActionPerformed
        test_position=liste.gibLänge()-1;
        liste.zumende();
        updateFields(liste.aktuellesElement());
    }//GEN-LAST:event_bnEndActionPerformed

    private void bnDelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bnDelActionPerformed
       liste.aktuellesEntfernen();
        test_position--;
        updateFields(liste.aktuellesElement());
       updateList();
    }//GEN-LAST:event_bnDelActionPerformed

    private void bnClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bnClearActionPerformed
       
       //updateFields();
       updateList();
    }//GEN-LAST:event_bnClearActionPerformed

    
    
    public void updateFields(Object akt){
        element=(VokabelElement) akt;
        if(testdir_d_e){
            tbDeutsch.setText(element.getDeutsch());
            tbDeutsch.setEnabled(false);
            tbEnglisch.setEnabled(true);
            tbEnglisch.setText("");
        } else {
            tbEnglisch.setText(element.getEnglisch());
            tbEnglisch.setEnabled(false);
            tbDeutsch.setEnabled(true);
            tbDeutsch.setText("");
        }
    }
    
    public void showError(String e){
        JOptionPane.showMessageDialog(null, e,"Error", JOptionPane.ERROR_MESSAGE);
    }
    
    public static void main(String args[]) {
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainVokabel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainVokabel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainVokabel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainVokabel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MainVokabel().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bnBack;
    private javax.swing.JButton bnBeginn;
    private javax.swing.JButton bnClear;
    private javax.swing.JButton bnDel;
    private javax.swing.JButton bnDir;
    private javax.swing.JButton bnEnd;
    private javax.swing.JButton bnFor;
    private javax.swing.JButton bnNew;
    private javax.swing.JButton bnTest;
    private javax.swing.JCheckBox cbShowVok;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lbStatus;
    private javax.swing.JTextField tbDeutsch;
    private javax.swing.JTextField tbEnglisch;
    private javax.swing.JTextPane tbVokabeln;
    // End of variables declaration//GEN-END:variables

    private void updateList() {
        if(!cbShowVok.isSelected()){
            return;
        }
        String t="";
        liste.zumAnfang();
        for(int i=0; i<liste.gibLänge();i++){
            VokabelElement e=(VokabelElement) liste.aktuellesElement();
            liste.naechstesElement();
            t+=i+" : "+e.getDeutsch()+" : "+e.getEnglisch()+"\n";
        }
        tbVokabeln.setText(t);
        for(int i=0;i<test_position;i++){
            liste.naechstesElement();
        }
        liste.goTo(test_position);
    }
}
